package com.xianzhiOA.service.impl;/**
 * Created by Administrator on 2017/5/11.
 */

import com.github.pagehelper.PageHelper;
import com.xianzhiOA.bean.XzEliminate;
import com.xianzhiOA.bean.XzInject;
import com.xianzhiOA.bean.XzQuality;
import com.xianzhiOA.dao.XzEliminateMapper;
import com.xianzhiOA.dao.XzInjectMapper;
import com.xianzhiOA.dao.XzQualityMapper;
import com.xianzhiOA.service.XzQualityService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作质量的业务层
 *
 * @author 甘汝雷
 * @create 2017-05-11 16:12
 **/
@Service("xzQualityService")
public class XzQualityServiceImpl implements XzQualityService {
    @Autowired
    private XzQualityMapper qualityMapper;
    @Autowired
    private XzInjectMapper injectMapper;
    @Autowired
    private XzEliminateMapper eliminateMapper;
    //添加工作质量信息
    @Override
    public int insert(XzQuality quality) {
        int off = 0,aut=0;//记录bug总数和bug清除数
        for (XzInject i:quality.getInjectList()){
            off += i.getInjectDesign()+i.getInjectCode()+i.getInjectTest();
        }
        for (XzEliminate e:quality.getEliminateList()){
            aut += e.getEliminateDesign()+e.getEliminateCode()+e.getEliminateTest();
        }
        quality.setBugSun(off);
        quality.setBugClear(aut);
        qualityMapper.insert(quality);
        int j = 1;
        for (XzInject i:quality.getInjectList()){
            i.setInjectGrade(j);
            i.setQualityId(quality.getQualityId());
            injectMapper.insert(i);
            j++;
        }
        j=1;
        for (XzEliminate e:quality.getEliminateList()){
            e.setEliminateGrade(j);
            e.setQualityId(quality.getQualityId());
            eliminateMapper.insert(e);
            j++;
        }
        return 0;
    }

    //删除工作质量信息
    @Override
    public int delete(Long qualityId) {
        return qualityMapper.delete(qualityId);
    }

    //伪删除
    @Override
    public int isdelete(Long qualityId) {
        Map map = new HashMap();
        map.put("qualityId",qualityId);
        map.put("isdelete",1);
        return qualityMapper.update(map);
    }

    //修改工作质量信息
    @Override
    public int update(XzQuality quality) {
        int off = 0,aut=0;//记录bug总数和bug清除数
        for (XzInject i:quality.getInjectList()){
            off += i.getInjectDesign()+i.getInjectCode()+i.getInjectTest();
            injectMapper.update(myIMap(i));
        }
        for (XzEliminate e:quality.getEliminateList()){
            aut += e.getEliminateDesign()+e.getEliminateCode()+e.getEliminateTest();
            eliminateMapper.update(myEMap(e));
        }
        quality.setBugSun(off);
        quality.setBugClear(aut);
        return qualityMapper.update(myMap(quality));
    }

    //把quality装换成map
    public Map myMap(XzQuality quality){
        Map map = new HashMap();
        map.put("qualityId",quality.getQualityId());
        map.put("projectName",quality.getProjectName());
        map.put("createTime",quality.getCreateTime());
        map.put("empName",quality.getEmpName());
        map.put("empName1",quality.getEmpName1());
        map.put("bugSun",quality.getBugSun());
        map.put("bugClear",quality.getBugClear());
        map.put("qualityOfficial",quality.getQualityOfficial());
        map.put("qualityAudit",quality.getQualityAudit());
        return map;
    }
    //把inject转换成map
    public Map myIMap(XzInject inject){
        Map map = new HashMap();
        map.put("injectId",inject.getInjectId());
        map.put("injectGrade",inject.getInjectGrade());
        map.put("qualityId",inject.getQualityId());
        map.put("injectDesign",inject.getInjectDesign());
        map.put("injectCode",inject.getInjectCode());
        map.put("injectTest",inject.getInjectTest());
        return map;
    }
    //把eliminate转换成map
    public Map myEMap(XzEliminate eliminate){
        Map map = new HashMap();
        map.put("eliminateId",eliminate.getEliminateId());
        map.put("eliminateGrade",eliminate.getEliminateGrade());
        map.put("qualityId",eliminate.getQualityId());
        map.put("eliminateDesign",eliminate.getEliminateDesign());
        map.put("eliminateCode",eliminate.getEliminateCode());
        map.put("eliminateTest",eliminate.getEliminateTest());
        return map;
    }
    //分页查询所有工作质量信息
    @Override
    public PageBean<XzQuality> selAll(XzQuality quality,int page,int rows, Date begin, Date end) {
        PageHelper.startPage(page,rows);
        Map map = myMap(quality);
        if (begin!=null)map.put("begin",begin);
        if (end!=null) map.put("end",end);
        List<XzQuality> qualityList = qualityMapper.selAll(map);
        PageBean pageBean = new PageBean(qualityList);
        return pageBean;
    }

    @Override
    public XzQuality selById(Long qualityId) {
        return qualityMapper.selById(qualityId);
    }
}
